﻿using A.M.ETO;
using A.M.Storage.Application;
using AutoMapper;
using DotNetCore.CAP;
using Newtonsoft.Json;
using Public_Class_Library;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Domain.Entity;

namespace Q.M.Finance.Application.Services
{
    public class ExpenditureServices : BaseServices<expenditure, string>, IExpenditureServices
    {
        private readonly IExpenditureRepository expenditureRepository;
        private readonly IMapper mapper;
        private readonly ICapPublisher capPublisher;
        private readonly IRecipient_informationRepository recipient_InformationRepository;
        private readonly IExpenditure_typeRepository expenditure_typeRepository;
        private readonly Cross_service_Helper cross_Service_Helper;
        public ExpenditureServices(IExpenditureRepository expenditureRepository, Cross_service_Helper cross_Service_Helper, IMapper mapper, ICapPublisher capPublisher, IRecipient_informationRepository recipient_InformationRepository, IExpenditure_typeRepository expenditure_typeRepository) : base(expenditureRepository)
        {
            this.expenditureRepository = expenditureRepository;
            this.mapper = mapper;
            this.capPublisher = capPublisher;
            this.recipient_InformationRepository = recipient_InformationRepository;
            this.expenditure_typeRepository= expenditure_typeRepository;
            this.cross_Service_Helper = cross_Service_Helper;
        }

        /// <summary>
        /// 添加支出管理
        /// </summary>
        /// <param name="expenditureAddDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetexpenditureAddInfo(expenditureAddDTO expenditureAddDTO)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                var expenditure = mapper.Map<expenditure>(expenditureAddDTO);
                var res = await expenditureRepository.AddInfor_Async(expenditure);
                if (res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加成功";
                    //添加成功需要传一个消息队列  传递哪个模块 还有编号
                    string numberName = "支出系统-" + expenditureAddDTO.invoiceid;
                    capPublisher.Publish("GetNewNumber", numberName);
                }
                else
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "添加失败";

                }
                return apiReturn;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改支出管理
        /// </summary>
        /// <param name="expenditureShowDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetexpenditureUpdateInfo(expenditureShowDTO expenditureShowDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var id = expenditureRepository.GetShowInfor(x => x.invoiceid.Equals(expenditureShowDTO.invoiceid));
            foreach (var item in id)
            {
                item.invoiceid = expenditureShowDTO.invoiceid;
                item.expenditure_theme = expenditureShowDTO.expenditure_theme;
                item.expenditure_typename = expenditureShowDTO.expenditure_typename;
                item.payment_date = expenditureShowDTO.payment_date;
                item.payee = expenditureShowDTO.payee;
                item.payment_amount = expenditureShowDTO.payment_amount;
                item.payee_bank_account = expenditureShowDTO.payee_bank_account;
            }
            var res = await expenditureRepository.UpdInfor_Batch_Async(id);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功";
            }
            return apiReturn;
        }
        /// <summary>
        /// 反填支出管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        public async Task<expenditure> FindexpenditureInfo(string? invoiceid)
        {
            Expression<Func<expenditure, bool>> i = c => c.invoiceid == invoiceid;
            var sales = expenditureRepository.GetShowObj(i);
            return sales;
        }

        /// <summary>
        /// 删除支出管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Deleteexpenditure(string? invoiceid)
        {
            Expression<Func<expenditure, bool>> i = c => c.invoiceid == invoiceid;
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var expenditure = expenditureRepository.GetShowObj(i);
            var list = await expenditureRepository.DelInfor_Async(expenditure);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }
        /// <summary>
        /// 支出管理显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageETO<expenditureShowDTO> GetexpenditureInfo(string? invoiceid, int pageIndex, int pageSize)
        {
            PageETO<expenditureShowDTO> pageETO = new PageETO<expenditureShowDTO>();
            Expression<Func<expenditure, bool>> wherexp = i => true;
            if (invoiceid != null)
            {
                wherexp = i => i.invoiceid == invoiceid;
            }
            var expenditurelist = expenditureRepository.GetShowInfor(wherexp);
            var payerlist = recipient_InformationRepository.GetShowInfor();
            var expendituretypelist = expenditure_typeRepository.GetShowInfor();
            var list = (from a in expenditurelist
                        join b in payerlist on a.payee equals b.payee
                        join c in expendituretypelist on a.expenditure_typename equals c.expenditure_typename
                        select new expenditureShowDTO
                        {
                            invoiceid = a.invoiceid,
                            expenditure_theme = a.expenditure_theme,
                            expenditure_typename = c.expenditure_typename,
                            payment_date = a.payment_date,
                            payee = b.payee,
                            payment_amount = a.payment_amount,
                            payee_bank_account = b.payee_bank_account,
                        }).ToList();
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
       

    }
}
